import type { PageLoad } from './$types';
import * as commands from '$lib/commands'
import { DateTime } from 'luxon'
import type { GetParams, IpcResponse, ListParams, Security, SecurityFilter, Kline, Klt } from '$lib/bindings';
import { updateHistoryStore, updateKlineRequest } from '$lib/FudaStore';



// 定义一个用于生成示例策略的函数
function generateSampleStrategy(id: any) {
    return {
        id: id,
        name: `策略${id}`,
        description: `这是一个示例策略${id}`,
        yield: `${(Math.random() * 1 / 2 + 10).toFixed(2)}% `,
        avgYield: `${(Math.random() * 1 / 2 + 1).toFixed(2)}% `, // 随机生成0.5%到1.5%之间的平均收益率
        maxYield: `${(Math.random() * 2 + 1 / 4).toFixed(2)}% `, // 随机生成1.25%到3.25%之间的最高收益率
        minYield: `${(Math.random() * -1 + 1 / 8).toFixed(2)}% `, // 随机生成-0.125%到0.125%之间的最低收益率
        topStocks: ['AAPL', 'GOOGL', 'TSLA', 'MSFT', 'AMZN'], // 示例Top 5股票代码
    };
}


var data: {
    id: string;

    strategies: any[]; // Replace 'any' with the appropriate type if known
} = {
    id: "null",

    strategies: [],
};

export const load: PageLoad = async ({ url, route, params }) => {
    const { id } = params;
    console.log("route=", route, " url=", url, " params=", params);
    data.id = id;
    try {
        // Define the argument
        const args: GetParams = {
            id: id, // "Tstg.DAY.0",
        }

        const res: IpcResponse<Security> = await commands.get_security(args)

        console.log("(security/[id]/+page.ts) get_security res=", res, " args=", args);

        if (res.error) {
            // If the response status is not OK, throw an error
            throw new Error(`(security/[id]/+page.ts) get_security Server returned an error: ${res.error}`);
        }
        if (res && res.result && res.result.data) {
            updateHistoryStore(null, res.result.data)
        }
    } catch (e) {
        console.log("(security/[id]/+page.ts) get_security error=", e);
    }

    updateKlineRequest(id, null, 0, null)


    // 使用for循环创建包含100个策略的数组
    const strategies = [];
    for (let i = 1; i <= 50; i++) {
        strategies.push(generateSampleStrategy(i));
    }

    try {
        return data;
    } catch (error) {
        console.error('Error calling Tauri command:', error);
        return data;
    } finally {

    }

};

